package com.tiannianbao.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;

/**
 * Created by Administrator on 2017/5/19.
 */
public class ZipUtil {

    /*
     * 解压zip文件到指定目录
     */
    public static void unzip(String path,String zipFile) throws ZipException, IOException {
        extractFolderExtra(path, zipFile, path);
    }
    /*
     * 删除目录
     */
    public static void removeFolder(File folder){
        if (folder.isFile()) {
            folder.delete();
            return;
        }
        if(folder.isDirectory()){
            File[] childFiles = folder.listFiles();
            if (childFiles == null || childFiles.length == 0) {
                folder.delete();
                return;
            }
            for (int i = 0; i < childFiles.length; i++) {
                removeFolder(childFiles[i]);
            }
            folder.delete();
        }
    }
    /*
     * 解压缩zip文件
     */
    public static void extractFolderExtra(String path,String zipFile,String targetPath) throws ZipException, IOException{
        // System.out.println(zipFile);
        int BUFFER = 2048;
        File file = new File(path + "/" + zipFile);
        ZipFile zip = new ZipFile(file);
        String newPath = targetPath;

        new File(newPath).mkdir();
        Enumeration zipFileEntries = zip.entries();

        // Process each entry
        while (zipFileEntries.hasMoreElements())
        {
            // grab a zip file entry
            ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
            String currentEntry = entry.getName();
            File destFile = new File(newPath+ "/" + currentEntry);
            //destFile = new File(newPath, destFile.getName());
            File destinationParent = destFile.getParentFile();

            // create the parent directory structure if needed
            destinationParent.mkdirs();

            if (!entry.isDirectory())
            {
                BufferedInputStream is = new BufferedInputStream(zip
                        .getInputStream(entry));
                int currentByte;
                // establish buffer for writing file
                byte data[] = new byte[BUFFER];

                // write the current file to disk
                FileOutputStream fos = new FileOutputStream(destFile);
                BufferedOutputStream dest = new BufferedOutputStream(fos,
                        BUFFER);

                // read and write until last byte is encountered
                while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
                    dest.write(data, 0, currentByte);
                }
                dest.flush();
                dest.close();
                is.close();
            }
        }
    }

    public static String convertStreamToString(InputStream is) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line).append("\n");
        }
        reader.close();
        return sb.toString();
    }

    public static String getStringFromFile (String filePath) throws Exception {
        File fl = new File(filePath);
        if(!fl.exists()){
            return null;
        }
        FileInputStream fin = new FileInputStream(fl);
        String ret = convertStreamToString(fin);
        //Make sure you close all streams.
        fin.close();
        return ret;
    }
}
